package com.rdzn.mall.center.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.rdzn.mall.center.entity.UmsProductCollect;
import com.rdzn.mall.center.mapper.UmsProductCollectMapper;
import com.rdzn.mall.center.service.IUmsProductCollectService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;

/**
 * @Description: 商品收藏
 * @Author: jeecg-boot
 * @Date:   2020-09-18
 * @Version: V1.0
 */
@Service
public class UmsProductCollectServiceImpl extends ServiceImpl<UmsProductCollectMapper, UmsProductCollect> implements IUmsProductCollectService {

    @Override
    public UmsProductCollect collect(Integer userId, Integer productId) {

        UmsProductCollect collect = getOne(new LambdaQueryWrapper<UmsProductCollect>().eq(UmsProductCollect::getUserId, userId).eq(UmsProductCollect::getProductId, productId));
        if (collect == null){
            collect = new UmsProductCollect();
            collect.setUserId(userId);
            collect.setProductId(productId);
            collect.setCreateTime(new Date());
            collect.setUpdateTime(new Date());
            collect.setIsAttention(0);
            save(collect);
            return collect;
        }else {
            removeById(collect.getId());
            return null;
        }
    }

    @Override
    public boolean collects(Integer userId, List<Integer> productIds) {
        if (CollectionUtils.isEmpty(productIds)){
            return false;
        }
        productIds.forEach(integer -> {
            UmsProductCollect collect = getOne(new LambdaQueryWrapper<UmsProductCollect>().eq(UmsProductCollect::getUserId, userId).eq(UmsProductCollect::getProductId, integer));
            if (collect == null){
                collect = new UmsProductCollect();
                collect.setUserId(userId);
                collect.setProductId(integer);
                collect.setCreateTime(new Date());
                collect.setUpdateTime(new Date());
                collect.setIsAttention(0);
                save(collect);
            }else {
                collect.setUpdateTime(new Date());
                updateById(collect);
            }
        });
        return true;
    }

    @Override
    public UmsProductCollect getOne(Integer userId, Integer productId) {
        List<UmsProductCollect> collects = list(new LambdaQueryWrapper<UmsProductCollect>().eq(UmsProductCollect::getUserId, userId).eq(UmsProductCollect::getProductId, productId));
        if (CollectionUtils.isNotEmpty(collects)){
            return collects.get(0);
        }
        return null;
    }

    @Override
    public List<UmsProductCollect> listByUserId(Integer userId) {
        return list(new LambdaQueryWrapper<UmsProductCollect>().eq(UmsProductCollect::getUserId, userId));
    }

    @Override
    public int countByUserId(Integer userId) {
        return count(new LambdaQueryWrapper<UmsProductCollect>().eq(UmsProductCollect::getUserId, userId));
    }
}
